//
//  Dc_HotViewController.m
//  艾薇贝儿
//
//  Created by dllo on 15/9/2.
//  Copyright (c) 2015年 dllo－－邴鹏超. All rights reserved.
//

#import "Dc_HotViewController.h"
#import "Dc_FhotTableViewCell.h"
#import "DcAFNetWorkTool.h"
#import "DcSongsModel.h"
#import "UIImageView+WebCache.h"
#import "DcFirstModel.h"
#import "Dc_PlayerSTK.h"
#import "Dc_Dan.h"
#import "AppDelegate.h"
#import "DcSongsModel.h"

@interface Dc_HotViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * _tableView;
    NSMutableArray * _array;
    NSMutableArray * _urlArray;
    NSMutableArray * _picArray;
}
@end

@implementation Dc_HotViewController
-(void)dealloc{
    [_array release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self http];
    
    UIImageView * image=[[UIImageView alloc]initWithFrame:self.view.frame];
    [image setImage:[UIImage imageNamed:@"hotcell.jpg"]];
    [image setUserInteractionEnabled:YES];
    
    _array =[[NSMutableArray alloc]init];
    _picArray=[[NSMutableArray alloc]init];
    _tableView =[[UITableView alloc]initWithFrame:self.view.frame];
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
     [self.view addSubview:_tableView];
    [_tableView setBackgroundView:image];
    [_tableView release];

    UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375*OffWidth, 180*OffHeight)];
    [imageView setImage:[UIImage imageNamed:@"hot1.jpg"]];
    
   
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.picUrl]];
    UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(30*OffWidth,130*OffHeight , 310*OffWidth,40*OffHeight)];
    label.text=self.name;
    label.textColor=[UIColor blackColor];
    [imageView addSubview:label];
    
    UILabel * auLabel=[[UILabel alloc]initWithFrame:CGRectMake(30*OffWidth, 90*OffHeight, 200*OffWidth, 25*OffHeight)];
    auLabel.text=self.author;
    auLabel.textColor=[UIColor blackColor];
    [imageView addSubview:auLabel];
    
    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0 , 0, 375*OffWidth, 180*OffHeight)];
    [view setBackgroundColor:[UIColor lightGrayColor]];
    [view addSubview:imageView];
    _tableView.tableHeaderView=view;
    
    [view release];
    [imageView release];
    [label release];
    [auLabel release];
    // Do any additional setup after loading the view.
}
-(void)http{
    
    
    
    
      NSString * url=[ NSString   stringWithFormat: @"http://api.songlist.ttpod.com/songlists/%@?f=0&os=4.4.2&alf=10001791&imei=111111111111111&from=android&resolution=480x854&net=2&api_version=1.0&agent=none&v=v8.1.1.2015081317&utdid=VV2s9r3zhNgDALDQVlZ6OMqc&longitude=0.0&user_id=0&latitude=0.0&language=zh",self.action[@"value"]];
   
    [DcAFNetWorkTool getUrl:url body:nil result:DcJSON headerFlile:nil success:^(id result) {
        NSMutableArray  * array=[result objectForKey:@"songs"];
        for (NSMutableDictionary * dic in array) {
            DcSongsModel * model=[[DcSongsModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_array addObject:model];
            
                    NSMutableDictionary * dic1=[result objectForKey:@"image"];
                    DcSongsModel * mod=[[DcSongsModel alloc]init];
                    [mod setValuesForKeysWithDictionary:dic1];
                    [_picArray addObject:mod];
                   
        }
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * cellId=@"cell";
    Dc_FhotTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell==0) {
        cell=[[Dc_FhotTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault                                   reuseIdentifier:cellId];
        cell.backgroundColor=[UIColor clearColor];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
  
    DcFirstModel * mo=[_array objectAtIndex:indexPath.row];
    cell.singerNmae.text=mo.singerName;
    cell.songName.text=mo.name;
    cell.nuLabel.text= [NSString  stringWithFormat:@"%d",indexPath.row+1];
    
    
       return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DcSongsModel * mo=[_picArray  objectAtIndex:indexPath.row];
    NSURL *url=[NSURL URLWithString:mo.pic];
    [Dc_Dan shareDan].image=url;
    
    DcFirstModel *lis = [_array objectAtIndex:indexPath.row];
    NSDictionary *dic = [lis.auditionList objectAtIndex:0];
    NSString *url1 = [dic objectForKey:@"url"];
    
    [Dc_Dan shareDan].url = url1;
    [Dc_Dan shareDan].singname = lis.name;
    [Dc_Dan shareDan].singername=lis.singerName;
    [Dc_Dan shareDan].index = indexPath.row;
    [Dc_Dan shareDan].urlarray = _array;
    
    
    if (lis.auditionList.count >2) {
        NSDictionary *dic = [lis.auditionList objectAtIndex:1];
        NSString *url = [dic objectForKey:@"url"];
        [self playerMusicUrl:url];
    }if (lis.auditionList.count==1) {
        NSDictionary *dic = [lis.auditionList objectAtIndex:0];
        NSString *url = [dic objectForKey:@"url"];
        [self playerMusicUrl:url];
    }
    
    
 
    
    
    
    
}
- (void)playerMusicUrl:(NSString *)url
{
    
    Dc_PlayerSTK *stk = [Dc_PlayerSTK shareHandleController];
    
    
    [stk play:url];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
